package com.demo1.servlet; /**
 * @author wzm
 * @create 2023-11-07 14:17
 * project_name:web2310
 * @Description
 */

import com.demo1.dao.UserDao;
import com.demo1.entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置请求和响应的编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //2.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //3.将请求参数封装为对象
        User user = new User(null,username,password);

        //4.访问dao进行数据的操作
        //查询用户是否存在
       UserDao userDao = new UserDao();
       User user1 =  userDao.selectByUserName(username);

       //5.判断用户对象是否为null
        if(user1 == null){
            //用户名不存在
            //执行新增
            userDao.add(user);
        }else{
            //用户名已经存在 各处提示信息
            response.getWriter().write("用户名已经存在，请更换！！");
        }


    }
}
